package com.mo.lib.ui.dialog

import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.mo.lib.R
import com.mo.lib.utils.setClick

/**
 * @ author：mo
 * @ data：2020/11/17:10:39
 * @ 功能：ios样式弹窗
 * 注：1、当控件样式如按钮颜色改变的时候，可继承此类，然后在构造方法中对对应的控件进行设置
 * 2、当所需视图与当前不同时，可继承此类，复写getLayoutId方法，返回自定义布局，自行进行设置，注意，自定义布局中必须包含此类中的5个控件id，不然会空指针
 */
open class BaseIosDialog() : BaseDialog(R.layout.dialog_ios) {
    override fun doWhat() {
    }

    /** 设置title  */
    @JvmOverloads fun setTitle(title: String, block: (TextView) -> Unit = {}): BaseIosDialog {
        val titleView = dialog.findViewById<TextView>(R.id.tv_dialog_ios_title)
        if (title.isEmpty()) {
            titleView.visibility = View.GONE
            return this
        }
        titleView.visibility = View.VISIBLE
        titleView.text = title
        block.invoke(titleView)
        return this
    }

    /** 设置内容 、内容排列方式 */
    @JvmOverloads fun setMsg(msg: String, block: (TextView) -> Unit = {}): BaseIosDialog {
        val msgView = dialog.findViewById<TextView>(R.id.tv_dialog_ios_msg)
        if (msg.isEmpty()) {
            msgView.visibility = View.GONE
            return this
        }
        msgView.visibility = View.VISIBLE
        msgView.text = msg
        block.invoke(msgView)
        return this
    }

    /** 设置内容 、内容排列方式 */
    @JvmOverloads fun setMsg2(msg: String, block: (TextView) -> Unit = {}): BaseIosDialog {
        val msgView2 = dialog.findViewById<TextView>(R.id.tv_dialog_ios_msg2)
        if (msg.isEmpty()) {
            msgView2.visibility = View.GONE
            return this
        }
        msgView2.visibility = View.VISIBLE
        msgView2.text = msg
        block.invoke(msgView2)
        return this
    }

    private val tv_dialog_ios_left: TextView by lazy {
        dialog.findViewById<TextView>(R.id.tv_dialog_ios_left)
    }
    private val tv_dialog_ios_line: TextView by lazy {
        dialog.findViewById<TextView>(R.id.tv_dialog_ios_line)
    }

    @JvmOverloads fun setLeftTextView(leftTxt: String, blockLeft: (View) -> Unit = {}): BaseIosDialog {
        if (leftTxt.isEmpty()) {
            tv_dialog_ios_left.visibility = View.GONE
            tv_dialog_ios_line.visibility = View.GONE
        } else {
            tv_dialog_ios_left.visibility = View.VISIBLE
            tv_dialog_ios_line.visibility = View.VISIBLE
            tv_dialog_ios_left.text = leftTxt
            tv_dialog_ios_left.setClick {
                blockLeft.invoke(this)
                dismisss()
            }
        }
        return this
    }

    @JvmOverloads fun setRightTextView(rightTxt: String, blockRight: (View) -> Unit = {}): BaseIosDialog {
        val tv_dialog_ios_right = dialog.findViewById<TextView>(R.id.tv_dialog_ios_right)
        val tv_dialog_ios_line = dialog.findViewById<TextView>(R.id.tv_dialog_ios_line)
        if (rightTxt.isEmpty()) {
            tv_dialog_ios_right.visibility = View.GONE
            tv_dialog_ios_line.visibility = View.GONE
        } else {
            tv_dialog_ios_right.text = rightTxt
            tv_dialog_ios_right.setClick {
                blockRight.invoke(this)
                dismisss()
            }
        }
        return this
    }


}
